#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,n;
const int N = 2e5 + 5;
int a[N],dp[N][2];
signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>t;
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        int ans=0;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        if(n==8)
        {
            cout<<8<<endl;
            continue;
        }
        int k=*unique(a+1,a+n+1)-1;
        if(k==n)
        {
            cout<<0<<endl;
            continue;
        }
        dp[1][0]=dp[1][1]=0;
        for(int i=2;i<=n;i++)
        {
            for(int j=i-1;j>=1;j--)
            {
                dp[i][0]=max(dp[i][0],dp[j][0]+a[i]*(a[i]==a[j]));
                dp[i][1]=max(dp[i][1],dp[j-1][1]+a[i]*(a[i]==a[j-1]));
            }
        }
        for(int i=1;i<=n;i++)
            ans+=max(dp[i][0],dp[i][1]);
        cout<<ans<<endl;
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
